package com.xjj.servicejob.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xjj.entity.Img;
import com.xjj.entity.Job;
import com.xjj.entity.Klass;
import com.xjj.entity.PageBean;
import com.xjj.servicejob.feign.KlassFeignClient;
import com.xjj.servicejob.feign.SchedulerFeignClient;
import com.xjj.servicejob.mapper.JobMapper;
import com.xjj.servicejob.mapper.JobPhotoMapper;
import com.xjj.servicejob.service.JobService;
import com.xjj.servicejob.vo.JobVo;
import com.xjj.vo.UpdateJobStatusVo;
import io.swagger.models.auth.In;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.GetCurrentTime;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sjy
 * @create 2022-10-20 11:29
 */
@Service
@Transactional
public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements JobService {
    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private JobPhotoMapper jobPhotoMapper;

    @Autowired
    private KlassFeignClient klassFeignClient;
    @Autowired
    private SchedulerFeignClient schedulerFeignClient;

    @Override
    public Integer add(Job job) {
        //设置初始值
        job.setSendTime(util.GetCurrentTime.getCurrentTime());
        job.setStatus("e");
        job.setSubmittedNum(0);
        //获取自动增加的主键
        jobMapper.insert(job);
        Klass klass = new Klass();
        klass.setId(job.getKlassId());
        klassFeignClient.addJobNumById(klass);
        if (job.getJobPhotos() != null && job.getJobPhotos().size() != 0) {
            job.getJobPhotos().forEach(img -> {
                img.setRelationalId(job.getId());
            });
            jobPhotoMapper.insert(job.getJobPhotos());
        }
        UpdateJobStatusVo updateJobStatusVo = new UpdateJobStatusVo();
        updateJobStatusVo.setId(job.getId());
        updateJobStatusVo.setDate(job.getEndTime()+" 00:00:00");
//        updateJobStatusVo.setDate("2022-10-21 10:33:30");
        System.out.println("date+"+updateJobStatusVo.getDate());
        System.out.println(updateJobStatusVo.getDate());
        try {
            schedulerFeignClient.updateJobStatus(updateJobStatusVo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return job.getId();
    }

    @Override
    public void updateJobById(Job job) {
        // 获取旧作业图片，进行批量删除
        List<Img> imgs = jobPhotoMapper.getAllImgByJobId(job.getId());
        if (imgs.size() != 0 && imgs != null) {
            List<Integer> ids = new ArrayList<>();
            imgs.forEach(i -> {
                ids.add(i.getId());
            });
            jobPhotoMapper.delete(ids, job.getId());
        }
        if (job.getJobPhotos() != null && job.getJobPhotos().size() != 0) {
            // 获取新作业图片，进行批量新增
            jobPhotoMapper.insert(job.getJobPhotos());
        }
        System.out.println(job);
        jobMapper.update(job);
    }

    @Override
    public void deleteById(Integer id) {
        //删除照片
        List<Img> imgs = jobPhotoMapper.getAllImgByJobId(id);
        if (imgs.size() != 0 && imgs != null) {
            List<Integer> ids = new ArrayList<>();
            imgs.forEach(i -> {
                ids.add(i.getId());
            });
            jobPhotoMapper.delete(ids, id);
        }
        jobMapper.deleteById(id);
    }

    @Override
    public PageBean<Job> getByPage(JobVo jobVo, Integer pageSize) {
        PageBean<Job> pageBean = new PageBean<>();
        Page p = PageHelper.startPage(jobVo.getPage(), pageSize);
        List<Job> data = jobMapper.getByCondition(jobVo.getJob());
        System.out.println("data：" + data);
        log.debug("jobs:{}" + data);
        pageBean.setData(data);
        pageBean.setCurrPage(jobVo.getPage());
        pageBean.setCurrPageSize(data.size());
        pageBean.setPageSize(pageSize);
        pageBean.setTotalNums((int) p.getTotal());
        pageBean.setTotalPages(p.getPages());
        return pageBean;
    }

    @Override
    public Job detail(Integer id) {
        Job job = jobMapper.selectById(id);
        List<Img> jobPhotos = jobPhotoMapper.getAllImgByJobId(id);
        job.setJobPhotos(jobPhotos);
        return job;
    }

    @Override
    public List<Job> getTodayAllJob(Integer klassId) {
        String currentDay = GetCurrentTime.getCurrentDay();
        System.out.println(currentDay.length());
        return jobMapper.getTodayAllJob(klassId,currentDay);
    }
}
